package com.wannengbang.cloudleader.homepage;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseFragment;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.RankingListBean;
import com.wannengbang.cloudleader.homepage.adapter.RankingListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 团队排行榜
 */
public class RankingTeamListFragment extends BaseFragment {
    public static final String ORDER_TITLE_NAME = "title";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    Unbinder unbinder;
    private String titleName;
    private String order_status;
    private List<RankingListBean.DataBean.ItemListBean> beanList;
    private RankingListAdapter listAdapter;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private SkeletonScreen skeletonScreen;
    private IHomePageModel homePageModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_team_ranking_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        Bundle bundle = getArguments();
        if (bundle != null) {
            titleName = bundle.getString(ORDER_TITLE_NAME);
            order_status = getType();
        }
        initView();
        initData();
        return view;
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
    }

    private void initView() {
        homePageModel = new HomePageModelImpl();
    }

    private void initData() {
        beanList = new ArrayList<>();
        listAdapter = new RankingListAdapter(beanList, order_status);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_2)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_product_list)
                .show();
        requestData();
    }


    public void requestData() {
        if ("1".equals(order_status)) {
            homePageModel.requestTeamShareToday(new DataCallBack<RankingListBean>() {
                @Override
                public void onSuccessful(RankingListBean bean) {
                    if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                        beanList.addAll(bean.getData().getItemList());
                    }
                    if (beanList.size() > 0) {
                        llNoData.setVisibility(View.GONE);
                    } else {
                        llNoData.setVisibility(View.VISIBLE);
                    }
                    listAdapter.notifyDataSetChanged();
                    skeletonScreen.hide();
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        }
        if ("2".equals(order_status)) {
            homePageModel.requestTeamTradeToday(new DataCallBack<RankingListBean>() {
                @Override
                public void onSuccessful(RankingListBean bean) {
                    if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                        beanList.addAll(bean.getData().getItemList());
                    }
                    if (beanList.size() > 0) {
                        llNoData.setVisibility(View.GONE);
                    } else {
                        llNoData.setVisibility(View.VISIBLE);
                    }
                    listAdapter.notifyDataSetChanged();
                    skeletonScreen.hide();
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        }
        if ("3".equals(order_status)) {
            homePageModel.requestTeamShareMonth(new DataCallBack<RankingListBean>() {
                @Override
                public void onSuccessful(RankingListBean bean) {
                    if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                        beanList.addAll(bean.getData().getItemList());
                    }
                    if (beanList.size() > 0) {
                        llNoData.setVisibility(View.GONE);
                    } else {
                        llNoData.setVisibility(View.VISIBLE);
                    }
                    listAdapter.notifyDataSetChanged();
                    skeletonScreen.hide();
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        }
        if ("4".equals(order_status)) {
            homePageModel.requestTeamTradeMonth(new DataCallBack<RankingListBean>() {
                @Override
                public void onSuccessful(RankingListBean bean) {
                    if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                        beanList.addAll(bean.getData().getItemList());
                    }
                    if (beanList.size() > 0) {
                        llNoData.setVisibility(View.GONE);
                    } else {
                        llNoData.setVisibility(View.VISIBLE);
                    }
                    listAdapter.notifyDataSetChanged();
                    skeletonScreen.hide();
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        }
    }

    private String getType() {
        String status = "0";
        switch (titleName) {
            case "今日激活":
                status = "1";
                break;
            case "今日交易":
                status = "2";
                break;
            case "上月激活":
                status = "3";
                break;
            case "上月交易":
                status = "4";
                break;
        }
        return status;
    }

}
